import Toast from '../../dist/toast/toast';
const db = wx.cloud.database()
Page({
  data: {
    userInfo: {
      avatarUrl: null,
      nickName: null
    },
    type:'0',
    columns: ['计算机', '机电', '自动化', '轻化', '信工', '土木与交通', '管理', '材能', '环境', '外国语', '应数', '物理', '艺设', '政法', '建筑城规', '经贸', '生物医学', '国际教育'],
    name: null,
    tip: null,
    free: null,
    content: null,
    image: "",
    imageURL: null,
    loading: null
  },
  onLoad:function(){
    wx.getSetting({
      success: res => {
        wx.getUserInfo({
          success: res => {
            this.setData({
              userInfo: {
                avatarUrl: res.userInfo.avatarUrl,
                nickName: res.userInfo.nickName
              }
            })
          }
        })

      }
    })
  },
  onChange(event) {
    let self = this;
    self.setData({
      type: event.detail.value
    })
  },
  handleCancel() {
    wx.switchTab({
      url: '../add/add'
    })
  },
  handleChooseImage() {
    let self = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success: (res) => {
        console.log(res) 
        self.setData({
          image: res.tempFilePaths[0]
        });
      }
    })
  },
  handleSubmit() {
    let self = this;
    let openid = wx.getStorageSync("openid")
    wx.getSetting({
      success: res => {
        wx.getUserInfo({
          success: function (res) {
            self.setData({
              userInfo: {
                avatarUrl: res.userInfo.avatarUrl,
                nickName: res.userInfo.nickName
              }
            })
          }
        })
      }
    })
    if (!(db.collection('user').where({
      '_openid': openid
    }))){
      wx.navigateTo({
        url: '../me/info/info'
      })
    }else if(!wx.getStorageSync("ifBind")){
        wx.navigateTo({
          url: '../me/bind/bind'
        })
    } else {
      if (self.data.name == null || self.data.content == null) {
        wx.showModal({
          title: '提示',
          showCancel: false,
          content: '请输入完整物品信息',
          success(res) {
            if (res.confirm) {
              return;
            }
          }
        })
      } else if (self.data.image == "") {
        wx.showModal({
          title: '提示',
          showCancel: false,
          content: '请上传物品图片',
          success(res) {
            if (res.confirm) {
              return;
            }
          }
        })
      } else {
        Toast.loading({
          mask: true,
          message: '上传中...'
        });
        wx.cloud.uploadFile({
          cloudPath: '' + (new Date()).valueOf() + '.png', // 上传至云端的路径
          filePath: self.data.image, // 小程序临时文件路径
          success: res => {
            db.collection('substance').add({
              // data 字段表示需新增的 JSON 数据
              data: {
                username: self.data.userInfo.nickName,
                userpic: self.data.userInfo.avatarUrl,
                name: self.data.name,
                date: (new Date()).valueOf(),
                free: self.data.free,
                content: self.data.content,
                fileID: res.fileID,
                type: self.data.type,
                tip: 'book'
              }
            })
              .then(res => {
                wx.showModal({
                  title: '提示',
                  showCancel: false,
                  content: '上传成功',
                  success(res) {
                    if (res.confirm) {
                      wx.navigateBack({
                        delta: 1
                      })
                    }
                  }
                })
              })
            console.log(res.fileID)
          },
          fail: console.log(self.data.image),
        })
      }
     
    }
  },
  handleName: function (e) {
    this.setData({
      name: e.detail.value
    })
  },
  handlePreview(e) {
    wx.previewImage({
      current: e.currentTarget.id,
      urls: [this.data.image]
    })
  },
  handleContent: function (e) {
    this.setData({
      content: e.detail.value
    })
  },
  handleAnon: function (e) {
    if (e.detail.value) {
      this.setData({
        free: '免费赠送'
      })
    }
    else {
      this.setData({
        free: null
      })
    }
  }
})